package com.donfu.web.controller.wxhouse;

import com.alibaba.fastjson.JSONObject;
import com.donfu.common.core.controller.BaseController;
import com.donfu.common.core.domain.AjaxResult;
import com.donfu.system.domain.house.HouseStore;
import com.donfu.system.domain.house.HouseStoreJoin;
import com.donfu.system.domain.house.HouseUser;
import com.donfu.system.service.house.IHouseStoreJoinService;
import com.donfu.system.service.house.IHouseStoreService;
import com.donfu.system.service.house.IHouseUserService;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 前台加盟管理
 */
@Api(tags = "前台加盟管理")
@RestController
@RequestMapping("/wx/storeJoin")
public class WebHouseStoreJoinController extends BaseController {

    @Autowired
    private IHouseUserService houseUserService;

    @Autowired
    private IHouseStoreJoinService houseStoreJoinService;

    @Autowired
    private IHouseStoreService houseStoreService;

    /**
     * 判断用户信息是否完善
     * @param userId
     * @return
     */
    @ApiOperation("判断用户信息是否完善")
    @GetMapping("/checkUserInfoComplete")
    public AjaxResult checkUserInfo(Long userId){
        HouseUser houseUser = houseUserService.selectHouseUserById(userId);
        if(houseUser!=null){
            Integer sex = houseUser.getSex();
            if (sex==null || (sex!=1 && sex!=2 )|| houseUser.getPhone()==null){
                return AjaxResult.error("个人信息未完善");
            }
            return AjaxResult.success(houseUser);
        }
        return AjaxResult.error("未找到该用户,请退出小程序后重新进入");
    }

    /**
     * 添加加盟数据
     * @param houseStoreJoin
     * @return
     */
    @ApiOperation("添加加盟数据")
    @PostMapping("/add")
    public AjaxResult addStore(@RequestBody HouseStoreJoin houseStoreJoin) {
        AjaxResult ajaxResult = checkUserInfo(houseStoreJoin.getUserId());
        String code = ajaxResult.get("code").toString();
        if (code.contains("50")){
            return ajaxResult;
        }

        // 将状态设置为审核中
        houseStoreJoin.setIsAllow(0);
        // 通过上面的信息审核后将状态设为已完善
        houseStoreJoin.setIsComplete(1);
        HouseUser houseUser=houseUserService.selectHouseUserById(houseStoreJoin.getUserId());
        houseStoreJoin.setUserName(houseUser.getWxNickName());
        houseStoreJoin.setUserPhone(houseUser.getPhone());
        houseStoreJoin.setAuditOpinion(null);
        // 根据店铺id获取店铺名称
        HouseStore store = houseStoreService.selectHouseStoreById(houseStoreJoin.getStoreId());
        houseStoreJoin.setStoreName(store.getName());
        // 添加新的加盟
        int i = houseStoreJoinService.insertHouseStoreJoin(houseStoreJoin);
        return toAjax(i);
    }

    /**
     * 根据用户id和加盟id删除店铺
     * @param userId
     * @param id
     * @return
     */
    @ApiOperation("删除加盟，当加盟失败时")
    @DeleteMapping
    public AjaxResult deleteStoreJoin(Long userId,Long id){
        // 查询加盟，只有未通过时才可以删除
        HouseStoreJoin houseStoreJoin = houseStoreJoinService.selectHouseStoreJoinById(id);
        Integer isAllow = houseStoreJoin.getIsAllow();
        if(isAllow!=2){
            return AjaxResult.error("加盟只有失败才可以删除");
        }
        int i=houseStoreJoinService.deleteHouseStoreJoinByIdAndUserId(id,userId);
        return toAjax(i);
    }

    /**
     * 前台获取加盟情况
     * @param userId
     * @return
     */
    @ApiOperation("前台查询加盟情况")
    @GetMapping("/getJoinInfo")
    public AjaxResult findJoinInfo(Long userId){
        HouseStoreJoin join=new HouseStoreJoin();
        join.setUserId(userId);
        List<HouseStoreJoin> houseStoreJoinList = houseStoreJoinService.selectHouseStoreJoinList(join);
        if(houseStoreJoinList==null || houseStoreJoinList.size()==0){
            return AjaxResult.success();
        }
        return AjaxResult.success(houseStoreJoinList.get(0));
    }

    @ApiOperation("/获取店铺列表")
    @GetMapping("/getStoreList")
    public AjaxResult getStoreList(String name){
        HouseStore store=new HouseStore();
        store.setName(name);
        List<HouseStore> houseStoreList = houseStoreService.selectHouseStoreList(store);
        return AjaxResult.success(houseStoreList);
    }
}
